/*
 * Copyright (C) 2008 Universidade Federal de Campina Grande
 *  
 * This file is part of OurGrid. 
 *
 * OurGrid is free software: you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the Free 
 * Software Foundation, either version 3 of the License, or (at your option) 
 * any later version. 
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details. 
 * 
 * You should have received a copy of the GNU Lesser General Public License 
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package org.ourgrid.worker.ui.async.gui;

import java.awt.Frame;

import javax.swing.ActionMap;
import javax.swing.JFrame;

import org.jdesktop.swingx.JXTaskPaneContainer;
import org.ourgrid.common.interfaces.status.WorkerCompleteStatus;
import org.ourgrid.worker.ui.async.gui.actions.InstallWorkerAsServiceAction;
import org.ourgrid.worker.ui.async.gui.actions.PauseWorkerAction;
import org.ourgrid.worker.ui.async.gui.actions.ResumeWorkerAction;
import org.ourgrid.worker.ui.async.gui.actions.StartWorkerAction;
import org.ourgrid.worker.ui.async.gui.actions.StopWorkerAction;
import org.ourgrid.worker.ui.async.gui.actions.UninstallWorkerServiceAction;
import org.ourgrid.worker.ui.async.model.WorkerAsyncUIListener;

/**
 * Panel that provide the actions to the worker.
 */
public class ActionsPanel extends javax.swing.JPanel implements WorkerAsyncUIListener {
    
	private static final long serialVersionUID = 1L;

	private static final String START_WORKER_ACTION = "worker.start.action";
	private static final String STOP_WORKER_ACTION = "worker.stop.action";
	private static final String PAUSE_WORKER_ACTION = "worker.pause.action";
	private static final String RESUME_WORKER_ACTION = "worker.resume.action";
	private static final String INSTALL_SERVICE_ACTION = "worker.install.action";
	private static final String UNINSTALL_SERVICE_ACTION = "worker.uninstall.action";
	
	
	private JXTaskPaneContainer jXTaskPaneContainer;
	private StartWorkerAction startAction;
	private StopWorkerAction stopAction;
	private ResumeWorkerAction resumeAction;
	private PauseWorkerAction pauseAction;
	private InstallWorkerAsServiceAction installAction;
	private UninstallWorkerServiceAction uninstallAction;
	
	
	/** Creates new form ActionsPanel */
    public ActionsPanel(Frame frame) {
    	initComponents();
        initActions(frame);
    }

    /**
     * Initializes the Actions that will appear at this panel.
     * @param frame The frame where this panel is, it is necessary to open
     * other panels on it.
     * @param componentClient The client of the peer.
     */
    private void initActions(Frame frame) {
    	
    	JFrame jFrame = (JFrame) frame;
    	startAction = new StartWorkerAction(jFrame.getContentPane());
    	stopAction = new StopWorkerAction(jFrame.getContentPane());
    	resumeAction = new ResumeWorkerAction();
    	pauseAction = new PauseWorkerAction();
    	controlTaskPane.add(startAction);
    	controlTaskPane.add(stopAction);
    	controlTaskPane.add(resumeAction);
    	controlTaskPane.add(pauseAction);
    	
    	installAction = new InstallWorkerAsServiceAction();
    	uninstallAction = new UninstallWorkerServiceAction();
    	otherOperationsPane.add(installAction);
    	otherOperationsPane.add(uninstallAction);
    	
	}
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jXTaskPaneContainer = new org.jdesktop.swingx.JXTaskPaneContainer();
        controlTaskPane = new org.jdesktop.swingx.JXTaskPane();
        otherOperationsPane = new org.jdesktop.swingx.JXTaskPane();

        controlTaskPane.setTitle("Control"); // NOI18N
        jXTaskPaneContainer.add(controlTaskPane);

        otherOperationsPane.setTitle("Other");
        jXTaskPaneContainer.add(otherOperationsPane);
        
        ActionMap actionMap = new ActionMap();
        actionMap.put(START_WORKER_ACTION, startAction);
        actionMap.put(STOP_WORKER_ACTION, stopAction);
        actionMap.put(PAUSE_WORKER_ACTION, pauseAction);
        actionMap.put(RESUME_WORKER_ACTION, resumeAction);
        actionMap.put(INSTALL_SERVICE_ACTION, installAction);
        actionMap.put(UNINSTALL_SERVICE_ACTION, uninstallAction);
        
        jXTaskPaneContainer.setActionMap(actionMap);
        
        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jXTaskPaneContainer, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jXTaskPaneContainer, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private org.jdesktop.swingx.JXTaskPane controlTaskPane;
    private org.jdesktop.swingx.JXTaskPane otherOperationsPane;
    // End of variables declaration//GEN-END:variables


    /**
     * Enable the actions that are permited when the
     * worker has been started.
     */
	public void workerStarted() {
		startAction.setEnabled(false);
		stopAction.setEnabled(true);
    	workerResumed();		
	}

	public void workerPaused() {
		pauseAction.setEnabled(false);
		resumeAction.setEnabled(true);
	}

	public void workerResumed() {
		pauseAction.setEnabled(true);
		resumeAction.setEnabled(false);
	}
	
    /**
     * Enable the actions that are permited when the
     * worker has been stopped.
     */
	public void workerStopped() {
		startAction.setEnabled(true);
		stopAction.setEnabled(false);
    	resumeAction.setEnabled(false);
    	pauseAction.setEnabled(false);
	}

	public void updateCompleteStatus(WorkerCompleteStatus completeStatus) {
		// TODO Auto-generated method stub
		
	}
	
	public void setStartEnabled(boolean enabled) {
		startAction.setEnabled(enabled);
	}

    
}
